﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Business.DAL
{
    /// <summary>
    /// 数据访问类System_TraceInfo。
    /// </summary>
    public class System_TraceInfo
    {
        public System_TraceInfo()
        { }
        #region  成员方法

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Business.Entity.System_TraceInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into System_TraceInfo(");
            strSql.Append("TraceId,OprContext,TraceType,TrackerId,TrackerName,TraceDepartId,TraceDepart,TraceTime)");

            strSql.Append(" values (");
            strSql.Append("@TraceId,@OprContext,@TraceType,@TrackerId,@TrackerName,@TraceDepartId,@TraceDepart,@TraceTime)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "TraceId", DbType.Guid, model.TraceId);
            db.AddInParameter(dbCommand, "OprContext", DbType.AnsiString, model.OprContext);
            db.AddInParameter(dbCommand, "TraceType", DbType.AnsiString, model.TraceType);
            db.AddInParameter(dbCommand, "TrackerId", DbType.Guid, model.TrackerId);
            db.AddInParameter(dbCommand, "TrackerName", DbType.AnsiString, model.TrackerName);
            db.AddInParameter(dbCommand, "TraceDepartId", DbType.Guid, model.TraceDepartId);
            db.AddInParameter(dbCommand, "TraceDepart", DbType.AnsiString, model.TraceDepart);
            db.AddInParameter(dbCommand, "TraceTime", DbType.DateTime, model.TraceTime);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Business.Entity.System_TraceInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update System_TraceInfo set ");
            strSql.Append("TraceId=@TraceId,");
            strSql.Append("OprContext=@OprContext,");
            strSql.Append("TraceType=@TraceType,");
            strSql.Append("TrackerId=@TrackerId,");
            strSql.Append("TrackerName=@TrackerName,");
            strSql.Append("TraceDepartId=@TraceDepartId,");
            strSql.Append("TraceDepart=@TraceDepart,");
            strSql.Append("TraceTime=@TraceTime");
            strSql.Append(" where TraceId=@TraceId and OprContext=@OprContext and TraceType=@TraceType and TrackerId=@TrackerId and TrackerName=@TrackerName and TraceDepartId=@TraceDepartId and TraceDepart=@TraceDepart and TraceTime=@TraceTime ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "TraceId", DbType.Guid, model.TraceId);
            db.AddInParameter(dbCommand, "OprContext", DbType.AnsiString, model.OprContext);
            db.AddInParameter(dbCommand, "TraceType", DbType.AnsiString, model.TraceType);
            db.AddInParameter(dbCommand, "TrackerId", DbType.Guid, model.TrackerId);
            db.AddInParameter(dbCommand, "TrackerName", DbType.AnsiString, model.TrackerName);
            db.AddInParameter(dbCommand, "TraceDepartId", DbType.Guid, model.TraceDepartId);
            db.AddInParameter(dbCommand, "TraceDepart", DbType.AnsiString, model.TraceDepart);
            db.AddInParameter(dbCommand, "TraceTime", DbType.DateTime, model.TraceTime);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid TraceId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from System_TraceInfo ");
            strSql.Append(" where TraceId=@TraceId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "TraceId", DbType.Guid, TraceId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Business.Entity.System_TraceInfo GetModel(Guid TraceId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select TraceId,OprContext,TraceType,TrackerId,TrackerName,TraceDepartId,TraceDepart,TraceTime from System_TraceInfo ");
            strSql.Append(" where TraceId=@TraceId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "TraceId", DbType.Guid, TraceId);
            Chenxin.Lib.Business.Entity.System_TraceInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select TraceId,OprContext,TraceType,TrackerId,TrackerName,TraceDepartId,TraceDepart,TraceTime ");
            strSql.Append(" FROM System_TraceInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "System_TraceInfo");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageIndex, int PageSize, string OrderField, string TableName, string strWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, strWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Business.Entity.System_TraceInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select TraceId,OprContext,TraceType,TrackerId,TrackerName,TraceDepartId,TraceDepart,TraceTime ");
            strSql.Append(" FROM System_TraceInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Business.Entity.System_TraceInfo> list = new List<Chenxin.Lib.Business.Entity.System_TraceInfo>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Business.Entity.System_TraceInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Business.Entity.System_TraceInfo model = new Chenxin.Lib.Business.Entity.System_TraceInfo();
            object ojb;
            ojb = dataReader["TraceId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.TraceId = new Guid(ojb.ToString());
            }
            model.OprContext = dataReader["OprContext"].ToString();
            model.TraceType = dataReader["TraceType"].ToString();
            ojb = dataReader["TrackerId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.TrackerId = new Guid(ojb.ToString());
            }
            model.TrackerName = dataReader["TrackerName"].ToString();
            ojb = dataReader["TraceDepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.TraceDepartId = new Guid(ojb.ToString());
            }
            model.TraceDepart = dataReader["TraceDepart"].ToString();
            ojb = dataReader["TraceTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.TraceTime = (DateTime)ojb;
            }
            return model;
        }

        #endregion  成员方法
    }
}